﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using QuickURLShortner.Common;
using QuickURLShortner.Common.Models;
using QuickURLShortner.Common.Utils;

namespace MMShortURL.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "QuickURLShortnerWCF" in code, svc and config file together.
    public class QuickURLShortnerAPI : IQuickURLShortnerAPI
    {       
        /// <summary>
        /// For Generating Short URL
        /// </summary>
        /// <param name="real_url">Original URL</param>
        /// <returns></returns>
        public string CreateUrl(string real_url)
        {
            Container oShortUrl = new Container();

            oShortUrl.RealUrl = real_url; //the OrigURL
            oShortUrl.ShortenedUrl = Utils.UniqueShortUrl();
            oShortUrl.CreateDate = DateTime.Now;
            oShortUrl.CreatedBy = "mm";

            Utils.AddUrlToDatabase(oShortUrl);

            var oShortenedUrl = Utils.PublicShortUrl(oShortUrl.ShortenedUrl);

            return oShortenedUrl.FirstPart+oShortenedUrl.SecondPart;
        }

        /// <summary>
        /// For the Actual URL
        /// </summary>
        /// <param name="short_url"></param>
        /// <returns></returns>
       public string GetUrl(string short_url)
        {
            short_url = Utils.InternalShortUrl(short_url);
            return Utils.RetrieveUrlFromDatabase(short_url).RealUrl;
        }       
    }
}
